import React from "react";
import { Row, Col, Switch, Breadcrumb, message, Modal } from "antd";
import {
  _updateRegisterCode,
  _selRegisterCode,
} from "../../util/api/user/index";

const { confirm } = Modal;
class PowerPage extends React.Component {
  state = {
    is_register: 0,
    power_id: 0,
  };
  getData = () => {
    _selRegisterCode().then((res) => {
      this.setState({
        is_register: res.data.data[0].is_register,
        power_id: res.data.data[0].power_id,
      });
    });
  };
  componentDidMount = () => {
    this.getData();
  };
  onHandleRegister = () => {
    let power_id = this.state.power_id;
    let val = this.state.is_register;
    let self = this;
    let temp = {};
    if (val) {
      confirm({
        title: "你确定要执行此操作吗？",
        // icon: <ExclamationCircleOutlined />,
        content: "关闭后用新户将无法注册",
        onOk() {
          temp.is_register = 0;
          _updateRegisterCode(power_id, temp).then((res) => {
            // console.log(res);
            if (res.status === 200 && res.data.code === 0) {
              message.success("关闭成功");
              self.getData();
            } else if (res.status === 200 && res.data.code === -1) {
              message.info(res.data.msg);
            }
          });
        },
        onCancel() {},
      });
      //授权
    } else {
      confirm({
        title: "你确定要执行此操作吗？",
        // icon: <ExclamationCircleOutlined />,
        content: "开启后用户可以注册",
        onOk() {
          temp.is_register = 1;
          _updateRegisterCode(power_id, temp).then((res) => {
            if (res.status === 200 && res.data.code === 0) {
              message.success("开启成功");
              self.getData();
            } else if (res.status === 200 && res.data.code === -1) {
              message.info(res.data.msg);
            }
          });
        },
        onCancel() {},
      });
    }
  };
  render() {
    return (
      <div>
        <Breadcrumb style={{ margin: "16px 0" }}>
          <Breadcrumb.Item>首页</Breadcrumb.Item>
          <Breadcrumb.Item>权限管理</Breadcrumb.Item>
        </Breadcrumb>
        <div className="admin-body" style={{ padding: 24, minHeight: 360 }}>
          <Row>
            <Col span={8}>
              <span style={{ marginRight: "1rem" }}>注册权限</span>
              <Switch
                checkedChildren="关闭"
                unCheckedChildren="开启"
                checked={this.state.is_register}
                onClick={this.onHandleRegister}
              />
            </Col>
            {/* <Col span={16}></Col> */}
          </Row>
        </div>
      </div>
    );
  }
}
export default PowerPage;
